<?php

class PublicHelper extends Helper {

    public function __construct() {
        
    }

    public function getTabFields($table) {
        $model = $this->LoadModel('Public');
        return $model->getTableFields($table);
    }

    public function getCardrt($card_val) {
        $data = Config::GetCrd();
        if (!Validator::isIdcard($card_val)) {
            return array('success' => false, 'msg' => '身份证号错误');
        }
        $card = array(
            'province' => $data[substr($card_val, 0, 2) . '0000'],
            'city' => $data[substr($card_val, 0, 4) . '00']
        );
        if (substr($card_val, 4, 2) != '00')
            $card['county'] = $data[substr($card_val, 0, 6)];
        if (!$card['province'])
            return array('success' => false, 'msg' => '身份证号错误');
        
        return array('success' => false, 'msg' => '获取成功', 'data' => $card);
    }

    /**
     * 报表导出
     * @param type $data 报表数据
     * @param type $report_name 报表名
     * @param type column 字段
     */
    public function downReport($data,$report_name,$column){
        $this->LoadResurces('PHPExcel/PHPExcel');
        $objPHPExcel = new PHPExcel();
        $name = array('unsecured'=>'未抵押报表','carfare'=>'车资未齐报表','overdue'=>'逾期报表','compensatory'=>'代偿报表','advance'=>'公司垫款明细表','telaudit'=>'电审通过明细表','proorder'=>'问题件报表','contract'=>'合同未收到报表','gps'=>'已垫款GPS未上线表');
        $zimu = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");
        $x = 0;
        foreach($column as $k => $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue($zimu[$x].'1', $v);
            $objPHPExcel->getActiveSheet()->getColumnDimension($zimu[$x])->setWidth(25);
            $i=0;
            foreach($data as $key => $value){
                if($k == 'o_vip_card' || $k == 'o_gps_no'){
                    $value[$k] = ' '.$value[$k];
                }
                $a = $i+2;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($zimu[$x].$a, $value[$k]);
                $i++;
            }
            $x++;
        }
        $objPHPExcel->getActiveSheet()->getStyle('A1:'.$zimu[$x-1].'1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('C0C0C0');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$name[$report_name].'.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save( 'php://output');

    }

}